package nxu.it.movieapi.service.pg.dao;

import nxu.it.api.model.Movie;
import nxu.it.api.model.SimpleMovie;
import org.jdbi.v3.sqlobject.config.RegisterBeanMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindMap;
import org.jdbi.v3.sqlobject.customizer.Define;
import org.jdbi.v3.sqlobject.statement.SqlQuery;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public interface MoviePgDao {
    @SqlQuery("SELECT * FROM movie WHERE id = :movieId")
    @RegisterBeanMapper(Movie.class)
    Optional<Movie> findById(@Bind("movieId") Integer movieId);

    @SqlQuery("SELECT * FROM movie " +
            "<where> " +
            "ORDER BY <orderBy> " +
            "LIMIT :size OFFSET :offset")
    @RegisterBeanMapper(SimpleMovie.class)
    List<SimpleMovie> complexPage(
            @Define("where") String whereClause,
            @Define("orderBy") String orderByClause,
            @Bind("size") int size,
            @Bind("offset") int offset,
            @BindMap Map<String, Object> params
    );

    @SqlQuery("SELECT COUNT(*) FROM movie " +
            "<where>")
    int complexCount(
            @Define("where") String whereClause,
            @BindMap Map<String, Object> params
    );

}